(function() {
    'use strict';

    angular
        .module('milkbeeApp')
        .controller('VoteDetailController', VoteDetailController);

    VoteDetailController.$inject = ['$scope', '$rootScope', '$stateParams', 'previousState', 'entity', 'Vote','AlertService'];

    function VoteDetailController($scope, $rootScope, $stateParams, previousState, entity, Vote,AlertService) {
        var vm = this;

        vm.vote = entity;
        vm.previousState = previousState.name;
        vm.cands=[];
        var unsubscribe = $rootScope.$on('milkbeeApp:questionnaireUpdate', function(event, result) {
            vm.questionnaire = result;
        });
        $scope.$on('$destroy', unsubscribe);
        
        this.save=function(){
        	//return Vote.PUT().$promise;
        	var vcandidates=[];
        	for(var i=0;i<vm.cands.length;++i){
        		if(vm.cands[i]==true){
        			vcandidates.push(vm.vote.candidates[i].id);
        		}
        	}
        	Vote.update({id:vm.vote.id,candidates:vcandidates}, onSaveSuccess, onSaveError);
        }
  
        
        function onSaveSuccess (result) {
           // $scope.$emit('milkbeeApp:cardUpdate', result);
            //$uibModalInstance.close(result);
            //vm.isSaving = false;
            AlertService.success('success');
        }

        function onSaveError () {
            //vm.isSaving = false;
        	 AlertService.error('error');
        }
    }
})();
